package com.cxy.controller.admin;

import com.cxy.dto.AllMessageDTO;
import com.cxy.dto.PageDTO;
import com.cxy.dto.UserDTO;
import com.cxy.result.PageResult;
import com.cxy.result.Result;
import com.cxy.service.UserService;
import com.cxy.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "党员信息管理")
@RestController("adminController")
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
   private UserService userService;
    /**
     * 党员分页查询
     */
    @ApiOperation("党员信息分页查询")
    @GetMapping("/page")
    public Result<PageResult> page( PageDTO pageDTO){
        log.info("分页查询:{}",pageDTO);
       PageResult pageResult = userService.page(pageDTO);
        return Result.success(pageResult);
    }
    /**
     * 增加用户
     */
    @ApiOperation("添加党员")
    @PostMapping("/save")
    public Result  saveUser(@RequestBody UserDTO userDTO){
        log.info("新增用户:{}",userDTO);
        userService.saveUser(userDTO);
        return Result.success();
    }

    /**
     * 根据id查询党员信息
      * @param id
     * @return
     */
    @ApiOperation("根据id查询党员")
    @GetMapping("/select/{id}")
    public Result<UserVO>getById(@PathVariable Long id){
        log.info("查询的id:{}",id);
        UserVO userVO = userService.getById(id);
        return Result.success(userVO);
    }
    /**
     * 修改党员信息功能
     */
    @ApiOperation("修改党员信息")
    @PutMapping("/update")
    public Result update(@RequestBody AllMessageDTO allMessageDTO){
        log.info("需要修改的信息:{}",allMessageDTO);
        userService.update(allMessageDTO);
        return Result.success();
    }
    /**
     * 重置用户密码
     */
    @ApiOperation("重置党员密码")
    @PutMapping("/updatePassword")
    public Result updatePassword( Long id){
        userService.updatePassword(id);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除党员")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Long>ids){
        log.info("要删除用户的id:{}",ids);
        userService.delete(ids);
        return Result.success();
    }

    /**
     * 导出用户模板表
     */
    @ApiOperation("导出党员用户模板表")
    @GetMapping("/export")
    public void export(HttpServletResponse response,HttpServletRequest request) throws IOException {
        userService.export(response,request);
    }
    /**
     * 导入用户数据
     */
    @ApiOperation("导入党员数据")
    @GetMapping("/import")
    public Result dataImport(MultipartFile file) throws IOException {
        System.out.println("文件大小:"+file.getSize());
        System.out.println("资源名称:"+file.getResource());
        userService.dataImport(file);
        return Result.success();
    }
//    /**
//     * 查询所有用户
//     */
//    @GetMapping("/select")
//    public Result<List<UserVO>> allSelect(){
//        List<UserVO> userVO = userService.select();
//        return Result.success(userVO);
//    }
}
